package nl.ceasaro.jeslee.core.model.clothes.size;

import nl.ceasaro.jeslee.core.model.AbstractEntity;
import org.apache.commons.lang.ArrayUtils;

/**
 * Created by IntelliJ IDEA.
 * User: cees
 * Date: Oct 7, 2008
 * Time: 8:52:44 PM
 * <p>
 * Sizes table for clothing
 * </p>
 * <pre>
 * WOMEN'S CLOTHING (dresses an suits)
 * american        4    6    8   10   12   14   16   18   20   22   24
 * European       34   36   38   40   42   44   46   48   50   52   54
 * British         6    8   10   12   14   16   18   20   22   24   26
 * Australia       8   10   12   14   16   18   20   22   24   26   28
 * Japan           7    9   11   13   15   17   19   21   23   25   27
 * <p/>
 *  WOMEN'S SHOES
 * american        5    6    7    8    9   10
 * European       36   37   38   39   40   41
 * British         4    5    6    7    8    9
 * <p/>
 *  CHILDREN'S CLOTHING
 * american        1    2    3    4    5    6   6X    7    8    9   10   11   12
 * European       80   92   98  104  110  116  122  122  128  134  140  146  152
 * British        18   20   22   24   26   28   30   32   34   36   38   40   42
 * <p/>
 *  CHILDREN'S SHOES
 * american        8    9   10   11   12   13    1    2    3
 * European       24   25   27   28   29   30   32   33   34
 * British         7    8    9   10   11   12   13    1    2
 * <p/>
 *  MEN'S SUITS
 * american       34   36   38   40   42   44   46   48
 * European       44   46   48   50   52   54   56   58
 * British        34   36   38   40   42   44   46   48
 * <p/>
 *  MEN'S SHIRTS
 * american       14½  15   15½  16   16½  17   17½  18
 * European       37   38   39   41   42   43   44   45
 * British        14½  15   15½  16   16½  17   17½  18
 * <p/>
 *  MEN'S SHOES
 * american        7    8    9   10   11   12   13
 * European       39½  41   42   43   44½  46   47
 * British         6    7    8    9   10   11   12
 * </pre>
 */
public abstract class ClothingSize extends AbstractEntity
{
   private static final long serialVersionUID = -3187872629655186143L;
   private String size;
   private ClothingSizeType clothingSizeType;

   protected ClothingSize(String size, ClothingSizeType clothingSizeType)
   {
      this.size = size;
      this.clothingSizeType = clothingSizeType;
   }

   public String getSize()
   {
      return size;
   }

   public void setSize(String size)
   {
      this.size = size;
   }

   public ClothingSizeType getClothingSizeType()
   {
      return clothingSizeType;
   }

   public void setClothingSizeType(ClothingSizeType clothingSizeType)
   {
      this.clothingSizeType = clothingSizeType;
   }

   protected abstract String[] giveAmericanSizes();

   protected abstract String[] giveEuropeanSizes();

   protected abstract String[] giveBritishSizes();

   protected abstract String[] giveAustralianSizes();

   protected abstract String[] giveJapansesSizes();

   public String asAmerican()
   {
      int sizeIndex = determineSizeIndex();
      String[] americanSizes = giveAmericanSizes();
      return findCorrespondingSize(sizeIndex, americanSizes);
   }

   public String asEuropean()
   {
      return findCorrespondingSize(determineSizeIndex(), giveEuropeanSizes());
   }

   public String asBritish()
   {
      return findCorrespondingSize(determineSizeIndex(), giveBritishSizes());
   }

   public String asAustralian()
   {
      return findCorrespondingSize(determineSizeIndex(), giveAustralianSizes());
   }

   public String asJapanese()
   {
      return findCorrespondingSize(determineSizeIndex(), giveJapansesSizes());
   }


   /**
    * @param index       the index within the array
    * @param stringArray the array
    * @return return the value of the element in the array at the specified index. If the array is <i>null</i> or
    *         it lenght is shorter (stringArray.length < index) an empty string is returned.
    */
   private static String findCorrespondingSize(int index, String[] stringArray)
   {
      String result = "";
      if (stringArray != null && index >= 0 && stringArray.length >= index)
      {
         result = stringArray[index];
      }
      return result;
   }

   private int determineSizeIndex()
   {
      String[] usedClothingSizes = new String[giveEuropeanSizes().length];
      switch (getClothingSizeType())
      {
         case AMERICAN:
            usedClothingSizes = giveAmericanSizes();
            break;
         case AUSTRALIAN:
            usedClothingSizes = giveAustralianSizes();
            break;
         case BRITSH:
            usedClothingSizes = giveBritishSizes();
            break;
         case EUROPEAN:
            usedClothingSizes = giveEuropeanSizes();
            break;
         case JAPANESE:
            usedClothingSizes = giveJapansesSizes();
            break;
      }
      return ArrayUtils.indexOf(usedClothingSizes, getSize());
   }


}
